﻿Shader "Custom/mAdditiveV2" {
Properties {
        _MainTex ("Particle Texture", 2D) = "white" {}
		_Position ("Position", Vector) = (0, 0, 0, 1)
		_Scale ("Scale", Vector) =  (1, 1, 1, 1)
		_ClipRange0 ("ClipRange0", Vector) =  (0, 0, 0, 0)
		_ClipArgs0 ("ClipArgs0", Vector) =  (1000, 1000, 1, 1)
    }

    Category {
        Tags { 
            "Queue"="Transparent"
            "IgnoreProjector"="True" 
            "RenderType"="Transparent"
        }
        Blend SrcAlpha One
		ColorMask RGB
        Cull Off 
        Lighting Off 
        ZWrite Off 
        Fog { Color (0,0,0,0) }
    
		SubShader {
			Pass {

				CGPROGRAM
				#pragma vertex vert
				#pragma fragment frag
				#pragma multi_compile _ SCALE_ON_Bill SCALE_ON_MESH
				#pragma multi_compile _ CLIP_ON

				#include "UnityCG.cginc"

				sampler2D _MainTex;
				
				// to adjust particle system Scale only use billlbord mode
				float4 _Position;
				float4 _Scale;

				// to adjust particle system clip range
				float4 _ClipRange0;
				float4 _ClipArgs0;
				
				struct appdata_t {
					float4 vertex : POSITION;
					half4 color : COLOR;
					float2 texcoord : TEXCOORD0;
				};

				struct v2f {
					float4 vertex : POSITION;
					half4 color : COLOR;
					float2 texcoord : TEXCOORD0;
					#ifdef CLIP_ON
					float2 worldPos : TEXCOORD1;
					#endif
				};
			
				

				v2f vert (appdata_t v)
				{
					v2f o;

					float4 adJustObjPos = v.vertex;
					#ifdef SCALE_ON_Bill
						adJustObjPos.xyz -= _Position.xyz;
						adJustObjPos.xyz = 
							float3( _Scale.x * adJustObjPos.x,
									_Scale.y * adJustObjPos.y,
									_Scale.z * adJustObjPos.z);
						adJustObjPos.xyz += _Position.xyz;
						o.vertex = mul(UNITY_MATRIX_MVP, adJustObjPos);
					#elif SCALE_ON_MESH
						adJustObjPos.xyz = 
							float3( _Scale.x * adJustObjPos.x,
									_Scale.y * adJustObjPos.y,
									_Scale.z * adJustObjPos.z);
						o.vertex = mul(UNITY_MATRIX_MVP, adJustObjPos);
					#endif
					o.vertex = mul(UNITY_MATRIX_MVP, adJustObjPos);

					o.color = v.color;
					o.texcoord = v.texcoord;
					
					#ifdef CLIP_ON
					o.worldPos = v.vertex.xy * _ClipRange0.zw + _ClipRange0.xy;
					#endif

					return o;
				}

				fixed4 frag (v2f i) : COLOR
				{	
					// 2.0f *
					fixed4 col =  i.color * tex2D(_MainTex, i.texcoord);
					
					// clip factor
					#ifdef CLIP_ON
					float2 factor = (float2(1.0, 1.0) - abs(i.worldPos)) * _ClipArgs0;
					col.a *= clamp( min(factor.x, factor.y), 0.0, 1.0);
					#endif

					return col;
				}
				ENDCG 
			}
		}
    }
}
